<?php
/**
 * @version 1.6
 * @package RealEstateManager LocationMap
 * @copyright 2009 OrdaSoft
 * @author 2009 Sergey Brovko-OrdaSoft(brovinho@mail.ru)
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @description Location map for RealEstateManager
*/
JHTML::_('behavior.mootools');
$pr = rand();
?>
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

if( !function_exists( 'sefreltoabs')) {
  function sefRelToAbs( $value ) {


    // Replace all &amp; with & as the router doesn't understand &amp;
    $url = str_replace('&amp;', '&', $value);//replace chars &amp; on & in 
    if(substr(strtolower($url),0,9) != "index.php") return $url;//cheking correct url
	
    $uri    = JURI::getInstance();
	 $prefix = $uri->toString(array('scheme', 'host', 'port'));
    return $prefix.JRoute::_($url);
  }
}

//Common parameters
$show_published = $params->def('only_published', 1);
//Individual parameters
$count_estates = intval($params->def('estates', 0));
$cat_id = $params->get('cat_id');
$estate_id = $params->get('estate_id');
$new_target = $params->def('new_target', 1);
$ItemId_tmp_from_params = $params->get('ItemId');
$moduleclass_sfx = $params->get('moduleclass_sfx');

$database = &JFactory::getDBO();

if ( !function_exists('getWhereUsergroupsString')) {
  function getWhereUsergroupsString( $table_alias ) { 
    global $my;
     
    if ( isset($my->id) AND $my->id != 0 ) { 
     
      $usergroups_sh = getGroupsByUser ($my->id,'');       
  
      //$usergroups_sh = '-2'.$usergroups_sh;   
      
    } else {
       $usergroups_sh = array ();
    }
    $usergroups_sh[] = -2; 
      
          
    $s = '';
    for ($i=0; $i<count($usergroups_sh); $i++) {  
      $g = $usergroups_sh[$i];
      $s .= " $table_alias.params LIKE '%,{$g}' or $table_alias.params = '{$g}' or $table_alias.params LIKE '{$g},%' or $table_alias.params LIKE '%,{$g},%' ";
      if ( ($i+1)<count($usergroups_sh) )
        $s .= ' or ';
    } 
    return $s;
  } 
}

$s=GetWhereUserGroupsString("c");

if ($show_published == 1 ) $sql_published = " AND h.published=1";
else $sql_published = "";

if ($cat_id != null && $estate_id != null)
echo ('<font color="#CC0000">You input IDs of categories and estates together! Correct this mistake.</font>');
else
{
   if($estate_id != null) $sql_where = " AND h.id IN(".$estate_id.")";
   if($cat_id != null) $sql_where = " AND c.id IN(".$cat_id.")";
   if($cat_id == null && $estate_id == null) $sql_where = "";

   $selectstring = "SELECT h.htitle,h.id,h.houseid,h.hlatitude,h.hlongitude,hc.idcat
                    \nFROM #__rem_houses AS h
                    \nLEFT JOIN #__rem_categories AS hc ON hc.iditem=h.id
		    \nLEFT JOIN #__rem_main_categories AS c ON c.id=hc.idcat
                    \nWHERE ($s) AND h.hlatitude IS NOT NULL".$sql_where.$sql_published.
                   "\nLIMIT ".$count_estates;
            $database->setQuery($selectstring);
            $rows= $database->loadObjectList();

$selectstring = "SELECT id  FROM #__menu WHERE menutype like '%menu%' AND link LIKE '%option=com_realestatemanager%'";
$database->setQuery($selectstring);
$ItemId_tmp_from_db = $database->loadResult();
if($ItemId_tmp_from_params==""){
  $ItemId_tmp=$ItemId_tmp_from_db;
}
else{
  $ItemId_tmp=$ItemId_tmp_from_params;
}
}
?>
<div class="realestatemanager_<?php if($moduleclass_sfx!='') echo $moduleclass_sfx; ?>">
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false" ></script>

<script type="text/javascript">
window.addEvent('domready', function() {
    var map;
    var marker = new Array();
    var myOptions = {
       zoom: 0,
       center: new google.maps.LatLng(0, 0),
       <?php if ($params->get('menu_map') == 0) echo "mapTypeControl: false,"; else echo "mapTypeControl: true,";?>
       <?php if ($params->get('control_map') == 0) echo "zoomControl: false, panControl: false, streetViewControl: false,";
	     else echo "zoomControl: true, panControl: true, streetViewControl: true,";?>
       mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map_canvas<?php echo $pr; ?>"), myOptions);
    var bounds = new google.maps.LatLngBounds ();
     <?php
     for ($i = 0; $i < count($rows); $i++)
     {
      if(isset($rows[$i]->hlatitude) & isset($rows[$i]->hlongitude) & $rows[$i]->hlatitude !='' & $rows[$i]->hlongitude !='')
        {
     ?>
	marker.push(new google.maps.Marker({
        position: new google.maps.LatLng(<?php echo $rows[$i]->hlatitude; ?>, <?php echo $rows[$i]->hlongitude; ?>),
        map: map,
	title: "<?php echo $rows[$i]->htitle; ?>"
	}));
	bounds.extend(new google.maps.LatLng(<?php echo $rows[$i]->hlatitude; ?>, <?php echo $rows[$i]->hlongitude; ?>));
	google.maps.event.addListener(marker[<?php echo $i; ?>], 'click', function() {
	if(<?php echo $new_target; ?> == 1) window.open("index.php?option=com_realestatemanager&task=view&id=<?php echo $rows[$i]->id; ?>&catid=<?php echo $rows[$i]->idcat;?>&Itemid=<?php echo $ItemId_tmp;?>");
	else document.location = "index.php?option=com_realestatemanager&task=view&id=<?php echo $rows[$i]->id; ?>&catid=<?php echo $rows[$i]->idcat;?>&Itemid=<?php echo $ItemId_tmp;?>";
	});
     <?php
     }
     ?>
     map.fitBounds(bounds);
     <?php
     }
     ?>
});
</script>

  <div id="map_canvas<?php echo $pr; ?>" style=
      "width: <?php echo $params->get('map_width');?>px; height: <?php echo $params->get('map_height'); ?>px;
      border: 1px solid black; float: rigth;" >
  </div>
<br>
<div style="text-align: center;"><a href="http://ordasoft.com" style="font-size: 10px;">Powered by OrdaSoft!</a></div>
</div>
